package com.loong.musicdemo.ui.video.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.loong.musicdemo.R;
import com.loong.musicdemo.model.MV;
import com.loong.musicdemo.adapter.NormalMVAdapter;
import com.loong.musicdemo.util.dataDeal.MvDeal;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 用于显示精彩MV页面,此页可根据地区\排序\分类等进行刷新
 */
public class WonderfulMvActivity extends AppCompatActivity {
    private static final int LIMIT = 10;//默认数据量
    private static int page = 0;//默认为第0页
    private static String area = "全部";//地区
    private static String type = "全部";//类型
    private static String sort = "上升最快";//排序方式
    private TextView searchText;//显示搜索方式
    private RadioGroup areaSelect, typeSelect, sortSelect;//对应地区选择,类型选择\排序方式选择
    private LinearLayout cancelSelect;//取消选择
    private Button select, finishedSelect;//筛选按钮,完成按钮
    private LinearLayout selectMVLinearLayout;//选择区域
    private RecyclerView recyclerViewMV;//显示数据
    private LinearLayout searchMV;//搜索
    @SuppressLint("StaticFieldLeak")
    private static SwipeRefreshLayout refreshMV;//下拉刷新
    @SuppressLint("StaticFieldLeak")
    private static SmartRefreshLayout loadMV;//上啦加载
    //MV 数据信息
    private static final List<MV> mvs = new ArrayList<>();
    private static final NormalMVAdapter adapter = new NormalMVAdapter(mvs);//适配器

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //背景图片融合
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        setContentView(R.layout.activity_wonderful_mv);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setTitle("");
            actionBar.setDisplayHomeAsUpEnabled(true);//若工具栏可用,则显示Home菜单
        }
        //初始化视图
        initView();
        //绑定各组件事件
        initEvent();
        //初始化原始数据绑定到RecyclerView
        recyclerViewMV.setAdapter(adapter);
        //数据初始化
        new InitMVData().execute();
    }

    /**
     * 绑定各组件事件
     */
    private void initEvent() {
        //显示选择页面
        select.setOnClickListener(v -> selectMVLinearLayout.setVisibility(View.VISIBLE));

        //隐藏选择页面
        cancelSelect.setOnClickListener(v -> selectMVLinearLayout.setVisibility(View.GONE));
        //下拉刷新
        refreshMV.setOnRefreshListener(() -> {
            page = 0;//清空页数
            new InitMVData().execute();//重新获取数据
        });
        //上啦加载
        loadMV.setOnLoadmoreListener(refreshLayout -> {
            new LoadNewMVData().execute();//加载后续数据
        });
        //搜索MV
        searchMV.setOnClickListener(v -> {
            Intent intent = new Intent(this, SearchMvActivity.class);
            startActivity(intent);
        });
        //完成选择,刷新数据
        finishedSelect.setOnClickListener(v -> {
            //显示搜索信息
            String builder = area +
                    "•" +
                    type +
                    "•" +
                    sort;
            searchText.setText(builder);
            page = 0;//清空页数
            //隐藏
            selectMVLinearLayout.setVisibility(View.GONE);
            //刷新数据源
            new InitMVData().execute();//重新获取数据
        });

        //三个radiogroup选择
        areaSelect.setOnCheckedChangeListener((group, checkedId) -> {
            //地区选择
            RadioButton button = findViewById(checkedId);
            area = button.getText().toString();
        });
        typeSelect.setOnCheckedChangeListener((group, checkedId) -> {
            //类型选择
            RadioButton button = findViewById(checkedId);
            type = button.getText().toString();
        });
        sortSelect.setOnCheckedChangeListener((group, checkedId) -> {
            //排序选择
            RadioButton button = findViewById(checkedId);
            sort = button.getText().toString();
        });
    }

    /**
     * 初始化视图
     */
    private void initView() {
        select = findViewById(R.id.select);
        selectMVLinearLayout = findViewById(R.id.mv_select);
        searchMV = findViewById(R.id.search);
        //三个radioGroup
        areaSelect = findViewById(R.id.area_select);
        typeSelect = findViewById(R.id.type_select);
        sortSelect = findViewById(R.id.sort_select);
        //默认第一个子项选中
        areaSelect.check(R.id.area_select_all);
        typeSelect.check(R.id.type_select_all);
        sortSelect.check(R.id.sort_select_fast);
        //显示搜索方式
        searchText = findViewById(R.id.search_text);
        //半透明区域
        cancelSelect = findViewById(R.id.cancel_select);
        //完成按钮
        finishedSelect = findViewById(R.id.finished_select);
        //显示数据的RecyclerView
        recyclerViewMV = findViewById(R.id.recycler_mv);
        //下拉刷新
        refreshMV = findViewById(R.id.refresh_mv);
        //上啦加载
        loadMV = findViewById(R.id.load_mv);
        loadMV.setEnableRefresh(false);//禁用下拉
        loadMV.autoLoadmore();//加载更多

    }


    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    static class InitMVData extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... voids) {
            //刷新数据
            mvs.clear();//清空原有数据
            List<MV> mvList = MvDeal.getMvByAllInfo(area, type, sort, LIMIT, page++);
            if (mvList != null && mvList.size() > 0) {
                mvs.addAll(mvList);//根据地区\类型\排序和数据数目获取数据
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            adapter.notifyDataSetChanged();//刷新数据
            refreshMV.setRefreshing(false);
        }
    }

    /**
     * 下拉加载新数据
     */
    static class LoadNewMVData extends AsyncTask<Void, Void, Boolean> {
        @Override
        protected Boolean doInBackground(Void... voids) {
            //刷新数据
            List<MV> mvList = MvDeal.getMvByAllInfo(area, type, sort, LIMIT, page++);
            if (mvList != null && mvList.size() > 0) {
                mvs.addAll(mvList);//根据地区\类型\排序和数据数目获取数据
                return true;//说明刷新数据成功
            }
            return false;//未进行数据刷新
        }

        @Override
        protected void onPostExecute(Boolean b) {
            super.onPostExecute(b);
            if (b) {
                adapter.notifyDataSetChanged();//刷新数据
            }
            loadMV.finishLoadmore();//停止加载显示
        }
    }
}